package org.opsli.modulars.business.iot.protocol;

import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class TestMain {

    public static void main(String[] args){
        /*String str = "3F8CCCCD";
        int ieee754Int = Integer.parseInt(str, 16);
        float realValue = Float.intBitsToFloat(ieee754Int);
        System.out.println(realValue);*/

       String s="00000148";
       System.out.println(covert("410CCCCD"));

    }

    /**
     * @param: [content]
     * @return: int
     * @description: 十六进制转十进制
     */
    public static int covert(String content){
        int number=0;
        String [] HighLetter = {"A","B","C","D","E","F"};
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0;i <= 9;i++){
            map.put(i+"",i);
        }
        for(int j= 10;j<HighLetter.length+10;j++){
            map.put(HighLetter[j-10],j);
        }
        String[]str = new String[content.length()];
        for(int i = 0; i < str.length; i++){
            str[i] = content.substring(i,i+1);
        }
        for(int i = 0; i < str.length; i++){
            number += map.get(str[i])*Math.pow(16,str.length-1-i);
        }
        return number;
    }


    //16进制浮点数转字符串，小端， 右高字节，如果收到的值为：00 12 23 45 则解析值为：23 45 00 12
    public static String sfloat(String str){
        String last = str.substring(4,8)+str.substring(0,4);
        if(!"".equals(last)) {
            Float f = Float.intBitsToFloat(new BigInteger(last, 16).intValue());
            DecimalFormat fnum = new DecimalFormat("##0.00");
            String dd = fnum.format(f);
            return dd;
        }
        return "--";
    }





    //16进制浮点数转字符串，大端，左高字节，直接解析：00 12 23 45
    private String bfloat(String str){
        Float f = Float.intBitsToFloat(new BigInteger(str, 16).intValue());
        DecimalFormat fnum = new DecimalFormat("##0.00");
        String dd = fnum.format(f);
        return dd;
    }

}
